package io.chenglicun.common.trans.core;

import cn.hutool.core.util.ReflectUtil;
import lombok.Getter;

@Getter
public class TransModel {

    private final TransField transField;

    /**
     * 需要被翻译的属性值
     */
    private final String transVal;

    /**
     * 当前对象
     */
    private final Object obj;

    public TransModel(Object obj, TransField field) {
        this.transField = field;
        this.obj = obj;
        Object fieldValue = ReflectUtil.getFieldValue(this.obj, this.transField.getTrans());
        this.transVal = fieldValue == null ? null : fieldValue.toString();
    }

    public void setValue(Object transVal) {
        Object fieldValue = ReflectUtil.getFieldValue(this.obj, this.transField.getField());
        if (fieldValue == null) {
            fieldValue = transVal;
        } else {
            fieldValue = fieldValue + "," + transVal;
        }
        ReflectUtil.setFieldValue(this.obj, this.transField.getField(), fieldValue);
    }

}
